---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}

```{r setup, include=FALSE, message=FALSE}

# Setup
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
pacman::p_load(here, argparse, knitr, glue, stringr, dplyr, tidyr, arrow, readr, purrr, kableExtra)

# Working directory and command line argument setup
current_script <- "generate_tables_and_figures/gen_benefit_cost_appendix_outputs/src/gen_benefit_cost_appendix_outputs.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))
task_dir <- here(dirname(dirname(file.path(current_script))))

knitr::opts_knit$set(root.dir = task_dir)
knitr::opts_chunk$set(include = TRUE, echo = FALSE, warning = FALSE, message = FALSE)
options(knitr.kable.NA = '', knitr.table.format = "latex", scipen = 999)

cl_args <- parse_make_args(c(
  "COST_TABLE",
  "CLEARANCE_RATE_TABLE",
  "REPORTING_RATE_TABLE",
  "OUT_DIR"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()
```

```{r}

source('R/benefit_cost_appendix_output_functions.R')

bca_tables_list <- list()
bca_tables_list$cost_table <- read_feather(cl_args$COST_TABLE) %>%
  select(violence_type, estimate_type, cost_2017) %>% 
  pivot_wider(id_cols = "violence_type", names_from = "estimate_type", values_from = "cost_2017") %>% 
  select(violence_type:offender_productivity_cost, bottom_up_cost,
         wtp_estimate_x_cj_x_offender_productivity_cost)
bca_tables_list$clearance_rate_table <- read_csv(cl_args$CLEARANCE_RATE_TABLE)
bca_tables_list$reporting_rate_table <- read_csv(cl_args$REPORTING_RATE_TABLE)

```

\begin{landscape}

```{r}
table <- print_bca_appendix_table(bca_tables_list)

table

timestamp <- get_timestamp()
writeLines(table, glue(cl_args$OUT_DIR, "/appendix_benefit_cost_{timestamp}.tex"))
writeLines(table, glue(cl_args$OUT_DIR, "/appendix_benefit_cost_up_to_date.tex"))

```

\end{landscape}

